package com.ubst.smartad.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.ubst.smartad.dao.TSARoleGroupDO;
import com.ubst.smartad.logger.Debug;

public class TSARoleGroupMngImpl {
	
	Connection con = null;
	private String serverName;
	private Debug debug;
	
	/** 
     * 생성자
     *   
    */ 
	public TSARoleGroupMngImpl(Connection con, String serverName, String log_level) {
		this.con = con;
		this.serverName = serverName;
		this.debug = new Debug("TSARoleGroupMngImpl", log_level);
	}
	
	/**
	 * 시스템에 역할 그룹을 생성하여 관리(REDOWL과 연동) 등록
	 * @param userAccountDO
	 * @throws SQLException
	 */
	public void insertRoleGroup(TSARoleGroupDO roleGroupDO) throws SQLException {
		PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        
        try {
            queryBuf = new StringBuffer();
			queryBuf.append("INSERT INTO TSA_ROLEGROUP " )
				.append(" ( ")
				.append("  SYSTEMID ,ROLEGID ,ROLEGNAME ,ROLEGAPPLY ,SCOMMENT ")
				.append(" ,CREATEDATE ,NSTATUS ,CRESULT ,LASTSYNC ")
				.append(" ) ")
				.append(" VALUES")
				.append(" ( ") 
				.append("  ? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ")
				.append(" ) ");

			debug.debug("insertRoleGroup ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, roleGroupDO.systemid);
			pstmt.setString(idx++, roleGroupDO.rolegid);
			pstmt.setString(idx++, roleGroupDO.rolegname);
			pstmt.setString(idx++, roleGroupDO.rolegapply);
			pstmt.setString(idx++, roleGroupDO.scomment);
			pstmt.setString(idx++, roleGroupDO.createdate);
			pstmt.setInt(idx++, roleGroupDO.nstatus);
			pstmt.setString(idx++, roleGroupDO.cresult);
			pstmt.setString(idx++, roleGroupDO.lastsync);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;
        } catch (SQLException e){
			throw e;
        } finally {
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }
	}
	
	/**
	 * 시스템에 역할 그룹을 생성하여 관리(REDOWL과 연동) 수정
	 * @param userAccountDO
	 * @throws SQLException
	 */
	public void updateRoleGroup(TSARoleGroupDO roleGroupDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {
            queryBuf = new StringBuffer();
			queryBuf.append("UPDATE TSA_ROLEGROUP " )
				.append(" SET ")
				.append("  SYSTEMID ,ROLEGID ,ROLEGNAME ,ROLEGAPPLY ,SCOMMENT ")
				.append(" ,CREATEDATE ,NSTATUS ,CRESULT ,LASTSYNC ")
				.append(" WHERE SYSTEMID=? AND ROLEGID=? ");

			debug.debug("updateRoleGroup ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, roleGroupDO.systemid);
			pstmt.setString(idx++, roleGroupDO.rolegid);
			pstmt.setString(idx++, roleGroupDO.rolegname);
			pstmt.setString(idx++, roleGroupDO.rolegapply);
			pstmt.setString(idx++, roleGroupDO.scomment);
			pstmt.setString(idx++, roleGroupDO.createdate);
			pstmt.setInt(idx++, roleGroupDO.nstatus);
			pstmt.setString(idx++, roleGroupDO.cresult);
			pstmt.setString(idx++, roleGroupDO.lastsync);
			pstmt.setString(idx++, roleGroupDO.systemid);
			pstmt.setString(idx++, roleGroupDO.rolegid);
            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }
	
	/**
	 * 시스템에 역할 그룹을 생성하여 관리(REDOWL과 연동) 목록 조회
	 * @param userid
	 * @return
	 * @throws SQLException
	 */
	public TSARoleGroupDO selectRoleGroup(String systemid, String rolegid) throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		TSARoleGroupDO roleGroupDO = null;

		try {
			
			StringBuffer queryBuf = new StringBuffer();
			queryBuf.append("SELECT ")
				.append("  SYSTEMID ,ROLEGID ,ROLEGNAME ,ROLEGAPPLY ,SCOMMENT ")
				.append(" ,CREATEDATE ,NSTATUS ,CRESULT ,LASTSYNC ")
				.append(" FROM TSA_ROLEGROUP ")
				.append(" WHERE SYSTEMID=? AND ROLEGID=? ");

			debug.debug("selectRoleGroupList ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, systemid);
			pstmt.setString(idx++, rolegid);

			rs = pstmt.executeQuery();

			if ( rs.next() ){
				roleGroupDO = new TSARoleGroupDO();
				roleGroupDO.systemid = systemid;
				roleGroupDO.rolegid = rolegid;
				roleGroupDO.rolegname = rs.getString("ROLEGNAME");
				roleGroupDO.rolegapply = rs.getString("ROLEGAPPLY");
				roleGroupDO.scomment = rs.getString("SCOMMENT");
				roleGroupDO.createdate = rs.getString("CREATEDATE");
				roleGroupDO.nstatus = rs.getInt("NSTATUS");
				roleGroupDO.cresult = rs.getString("CRESULT");
				roleGroupDO.lastsync = rs.getString("LASTSYNC");
			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return roleGroupDO;
	}
	
	/**
	 * 시스템에 역할 그룹을 생성하여 관리(REDOWL과 연동) 삭제
	 * @param userid
	 * @throws SQLException
	 */
	public void deleteRoleGroup(String systemid, String rolegid) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

            queryBuf = new StringBuffer();
			queryBuf.append("DELETE FROM TSA_ROLEGROUP " )
				.append(" WHERE SYSTEMID=? AND ROLEGID=? ");

			debug.debug("deleteRoleGroup ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, systemid);
			pstmt.setString(idx++, rolegid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }
}
